package com.ha.garbage;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;
import com.ha.garbage.model.Result;
import com.ha.garbage.model.UserLoginVO;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MenuActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private GiftFragment giftFragment;
    private GarbageFragment garbageFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_menu);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        //初始化控件
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        //默认首页选中
        selectedFragment(0);
//        bottomNavigationView的点击事件

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                if(menuItem.getItemId() == R.id.home){
                    selectedFragment(0);
                }else if(menuItem.getItemId() == R.id.gift){
                    selectedFragment(1);
                }else if(menuItem.getItemId() == R.id.garbage){
                    selectedFragment(2);
                }else {
                    selectedFragment(3);
                }
                return true;
            }
        });

    }

    private void selectedFragment(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //隐藏
        hideFragment(fragmentTransaction);
        if(position == 0){
            if(homeFragment == null ){
                homeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content,homeFragment);
            }else {
                fragmentTransaction.show(homeFragment);
            }

        }else if(position == 1){
            if(giftFragment == null ){
               giftFragment =  new GiftFragment();
               fragmentTransaction.add(R.id.content,giftFragment);
            }else {
                fragmentTransaction.show(giftFragment);
            }
        }else if (position == 2){
            if(garbageFragment == null){
               garbageFragment = new GarbageFragment();
                fragmentTransaction.add(R.id.content,garbageFragment);
            }else {
                fragmentTransaction.show(garbageFragment);
            }
        }else {
            if(mineFragment == null ){
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content,mineFragment);
            }else {
                fragmentTransaction.show(mineFragment);
            }
        }
        //提交选项
        fragmentTransaction.commit();
    }
    private void hideFragment( FragmentTransaction fragmentTransaction){
        if(homeFragment!=null){
            fragmentTransaction.hide(homeFragment);
        }
        if(giftFragment != null ){
            fragmentTransaction.hide(giftFragment);
        }
        if(garbageFragment != null ){
            fragmentTransaction.hide(garbageFragment);
        }
        if(mineFragment !=null ){
            fragmentTransaction.hide(mineFragment);
        }
    }
}